/*
 * 项目的配置信息
 */
import { defineStore } from 'pinia';
import { appDefaultConfig } from '@/config/app-config';
import localStorageKeyConst from '@/constants/local-storage-key-const';
import { AppConfig } from '@/types/config';
import { localRead } from '@/utils/local-util';

let state: AppConfig = { ...appDefaultConfig };

let appConfigStr = localRead(localStorageKeyConst.APP_CONFIG);
let language = appDefaultConfig.language;
if (appConfigStr) {
  try {
    state = JSON.parse(appConfigStr);
    language = state.language;
  } catch (e) {
    console.error(e);
  }
}

/**
 * 获取初始化的语言
 */
export const getInitializedLanguage = function () {
  return language;
};

// 导出一个名为useAppConfigStore的常量，该常量是一个defineStore函数的返回值
export const useAppConfigStore = defineStore({
  id: 'appConfig',
  state: (): AppConfig => ({
    // 读取config下的默认配置
    ...state,
  }),
  actions: {
    reset(): void {
      this.$state = { ...appDefaultConfig };
    },
    showHelpDoc(): void {
      this.helpDocFlag = true;
    },
    hideHelpDoc(): void {
      this.helpDocFlag = false;
    },
  },
});
